<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/6
 * Time: 22:16
 */

namespace app\admin\model;


use think\Model;

class Changyong extends Model
{
    protected $name = 'member_chang';
    protected $update = ['updatetime'];

    public function getlist(){
        $param = input();
        $where = [];
        if (isset($param['real_name'] )&& !empty($param['real_name'])) $where['b.real_name'] =['like','%'.$param['real_name'].'%'];
        if (isset($param['phone'] )&& !empty($param['phone'])) $where['b.phone'] =$param['phone'];
        return self::alias('a')->join('member b', 'a.cid = b.id')->where($where)->field('b.real_name,b.phone,a.beizu,a.id')->paginate(10);
    }

    public function add(){
        try{
            $param = input();
            if (self::where(['cid' => $param['cid']])->find()){
                throw new \Exception('该用户已经存在，请重新选择!');
            }
            if (self::save($param)){
                return msg(1,url('memberlist'),'提交成功');
            }
        }catch (\Exception $e){
            return msg(-1,[],$e->getMessage());
        }
    }

    public function edit(){
        try{
            $param =input();
            $m = self::get($param['id']);
            $m->beizu = $param['beizu'];
            if ($m->save()){
                return msg(1,url('memberlist') ,'提交成功');
            }
        }catch (\Exception $e){
            return msg(-1,[],$e->getMessage());
        }
    }

    public function hasmenber(){
        return $this->hasOne('Member','id','cid')->field('real_name,phone');
    }
    public function setUpdatetimeAttr()
    {
        return time();
    }


}